package the.broccoli.et.interfaces.controller;

import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;
import org.wisteria.framework.web.APIResult;
import org.wisteria.framework.web.QueryResult;
import the.broccoli.auth.UserContext;
import the.broccoli.et.application.service.EventTriggerService;
import the.broccoli.et.interfaces.dto.EventTriggerCondition;
import the.broccoli.et.interfaces.dto.EventTriggerDTO;
import the.broccoli.et.interfaces.dto.EventTriggerSavingDTO;

import javax.annotation.Resource;

/**
 * @author lei.liu
 * @date 2021/7/20
 */
@RestController
@RequestMapping("/et/event/trigger")
public class EventTriggerController {

    @Resource
    private EventTriggerService eventTriggerService;

    @PostMapping("/save")
    public APIResult<Boolean> saveEventTrigger(EventTriggerSavingDTO eventTrigger) {
        eventTrigger.setUserId(UserContext.userId());
        return APIResult.success(eventTriggerService.saveEventTrigger(eventTrigger.toEventTrigger()));
    }

    @GetMapping("/page")
    public APIResult<QueryResult<EventTriggerDTO>> page(
            @ApiParam(name = "keyword", value = "关键字", required = false)
            @RequestParam(value = "keyword", required = false) String keyword,
            @ApiParam(name = "orderBy", value = "排序字段", required = false)
            @RequestParam(value = "orderBy", required = false) String orderBy,
            @ApiParam(name = "pageNum", value = "页码", required = false, defaultValue = "1")
            @RequestParam(value = "pageNum", required = false) Integer pageNum,
            @ApiParam(name = "pageNum", value = "每页大小", required = false, defaultValue = "20")
            @RequestParam(value = "pageSize", required = false) Integer pageSize
    ) {
        EventTriggerCondition condition = new EventTriggerCondition();
        condition.setUserId(condition.getUserId());
        condition.setKeyword(keyword);
        condition.setOrderBy(orderBy);
        condition.setPageNum(pageNum);
        condition.setPageSize(pageSize);
        return APIResult.success(eventTriggerService.pageList(condition));
    }
}
